//
//  MOGetMarriedListEditOrCreatViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/8/16.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOGetMarriedListEditOrCreatViewController.h"
#import "MOGetMarriedListEditOrCreatViewCell.h"

@interface MOGetMarriedListEditOrCreatViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)UIView *bottomView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)NSDictionary *dataDic;

@end

@implementation MOGetMarriedListEditOrCreatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    [self initData];
    
    if ([self.marriedTypeStr integerValue]==2) {
        [self getData];
    }
}

-(void)creatView
{
    self.title = [self.marriedTypeStr integerValue]==1?@"添加结亲汇报":@"编辑结亲汇报";
    
    self.view.backgroundColor = KGrayColor_f8f8f8;
    
    [self.view addSubview:self.tableView];
    
    [self.view addSubview:self.bottomView];
}

-(void)initData
{
    self.dataSource = [NSMutableArray array];
    NSArray *arr = @[
                     @[@"牵头处室",@"时间批次",@"干部职工走访人数",@"干部职工入住人数",@"入住结亲群众户数"],
                     @[@"走访户数",@"走访人数"],
                     @[@"宣讲次数",@"参加人数"],
                     @[@"捐款(元)",@"捐物(件)",@"就医(件)",@"就学(件)",@"就业(件)",@"发展生产",@"其他(件)",@"合计(件)",@"惠及各族群众户数"],
                     @[@"项目",@"场次",@"实际参加人/次"]
                     ];
    NSNumber *w1 = @70;
    NSNumber *w2 = @100;
    NSNumber *w3 = @140;
    NSNumber *w4 = @50;
    NSArray *wArr = @[
                     @[w1,w1,w3,w3,w3],
                     @[w1,w1],
                     @[w1,w1],
                     @[w1,w1,w1,w1,w1,w1,w1,w1,w3],
                     @[w4,w4,w2]
                     ];
    NSArray *typeA = @[
                       @[@"0",@"0",@"4",@"4",@"4"],
                       @[@"4",@"4"],
                       @[@"4",@"4"],
                       @[@"8",@"4",@"4",@"4",@"4",@"4",@"4",@"4",@"4"],
                       @[@"0",@"4",@"4"]
                       ];
    NSArray *vArr = @[
                      @[@"请输入牵头处室",@"请输入时间批次",@"请输入干部职工走访人数",@"请输入干部职工入住人数",@"请输入入住结亲群众户数"],
                      @[@"请输入走访户数",@"请输入走访人数"],
                      @[@"请输入宣讲次数",@"请输入参加人数"],
                      @[@"请输入捐款金额",@"请输入捐物件数",@"请输入就医件数",@"请输入就学件数",@"请输入就业件数",@"请输入发展生产件数",@"请输入其他件数",@"请输入合计件数",@"请输入惠及各族群众户数"],
                      @[@"请输入项目",@"请输入场次",@"请输入实际参加人/次"]
                      ];
    for (int i = 0; i<arr.count; i++) {
        NSArray *subArray = arr[i];
        NSMutableArray *subMuArr = [NSMutableArray array];
        for (int j = 0; j<subArray.count; j++) {
            MOGetMarriedListEditOrCreatModel *model = [[MOGetMarriedListEditOrCreatModel alloc]init];
            model.nameStr = subArray[j];
            model.placeStr = vArr[i][j];
            model.keyBoardType = typeA[i][j];
            model.nameLabelWidth = [wArr[i][j] floatValue];
            
            [subMuArr addObject:model];
        }
        [self.dataSource addObject:subMuArr];
    }
    [self.tableView reloadData];
}

-(void)getData
{
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",RECIPIENTREPORT_GETRECIPIENTREPORTBYID_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    NSDictionary *dic = @{
                          @"id":self.idStr,
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:dic success:^(id responseObject) {
        NSLog(@"结亲汇报详情：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            
            self.dataDic = responseObject;
            for (int i = 0; i<self.dataSource.count; i++) {
                NSArray *subArray = self.dataSource[i];
                for (int j = 0; j<subArray.count; j++) {
                    MOGetMarriedListEditOrCreatModel *model = subArray[j];
                    if (i==0) {
                        if (j==0) {
                            model.valueStr = [NSString passNullForString:responseObject[@"leadDep"]];
                        }else if (j==1){
                            model.valueStr = [NSString passNullForString:responseObject[@"timeBatch"]];
                        }else if (j==2){
                            model.valueStr = [NSString passNullForString:responseObject[@"staffNum"]];
                        }else if (j==3){
                            model.valueStr = [NSString passNullForString:responseObject[@"staffLiveNum"]];
                        }else if (j==4){
                            model.valueStr = [NSString passNullForString:responseObject[@"liveFamilyNum"]];
                        }
                    }else if (i==1){
                        if (j==0) {
                            model.valueStr = [NSString passNullForString:responseObject[@"visitFamilyNum"]];
                        }else if (j==1){
                            model.valueStr = [NSString passNullForString:responseObject[@"visitNum"]];
                        }
                    }else if (i==2){
                        if (j==0) {
                            model.valueStr = [NSString passNullForString:responseObject[@"preachNum"]];
                        }else if (j==1){
                            model.valueStr = [NSString passNullForString:responseObject[@"joinNum"]];
                        }
                    }else if (i==3){
                        if (j==0) {
                            model.valueStr = [NSString passNullForString:responseObject[@"donation"]];
                        }else if (j==1){
                            model.valueStr = [NSString passNullForString:responseObject[@"goods"]];
                        }else if (j==2){
                            model.valueStr = [NSString passNullForString:responseObject[@"doctor"]];
                        }else if (j==3){
                            model.valueStr = [NSString passNullForString:responseObject[@"goSchool"]];
                        }else if (j==4){
                            model.valueStr = [NSString passNullForString:responseObject[@"getJob"]];
                        }else if (j==5){
                            model.valueStr = [NSString passNullForString:responseObject[@"yield"]];
                        }else if (j==6){
                            model.valueStr = [NSString passNullForString:responseObject[@"other"]];
                        }else if (j==7){
                            model.valueStr = [NSString passNullForString:responseObject[@"total"]];
                        }else if (j==8){
                            model.valueStr = [NSString passNullForString:responseObject[@"discountsFamily"]];
                        }
                    }else if (i==4){
                        if (j==0) {
                            model.valueStr = [NSString passNullForString:responseObject[@"fellowship"]];
                        }else if (j==1){
                            model.valueStr = [NSString passNullForString:responseObject[@"fellowshipNum"]];
                        }else if (j==2){
                            model.valueStr = [NSString passNullForString:responseObject[@"actualNum"]];
                        }
                    }
                }
            }
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"结亲汇报详情：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

#pragma mark -- 提交
-(void)clickSubmitButton
{
    //判空
    NSArray *vArr = @[
                      @[@"请输入牵头处室",@"请输入时间批次",@"请输入干部职工走访人数",@"请输入干部职工入住人数",@"请输入入住结亲群众户数"],
                      @[@"请输入走访户数",@"请输入走访人数"],
                      @[@"请输入宣讲次数",@"请输入参加人数"],
                      @[@"请输入捐款金额",@"请输入捐物件数",@"请输入就医件数",@"请输入就学件数",@"请输入就业件数",@"请输入发展生产件数",@"请输入其他件数",@"请输入合计件数",@"请输入惠及各族群众户数"],
                      @[@"请输入项目",@"请输入场次",@"请输入实际参加人/次"]
                      ];
    NSMutableArray *endDataArray = [NSMutableArray array];
    for (int i = 0; i<vArr.count; i++) {
        NSArray *subArray = self.dataSource[i];
        NSArray *subArray1 = vArr[i];
        NSMutableArray *subDataAr = [NSMutableArray array];
        for (int j = 0; j<subArray.count; j++) {
            MOGetMarriedListEditOrCreatModel *model = subArray[j];
            if (model.valueStr.length==0) {
                [[ADShowMessage shareShowMessage]showTextOnly:subArray1[j] showInView:self.view];
                return;
            }
            [subDataAr addObject:model.valueStr];
        }
        [endDataArray addObject:subDataAr];
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:[MOUser sharedMOUser].userName forKey:@"userId"];
    if (self.dataDic.allKeys.count>0) {//编辑
        [dic setObject:self.idStr forKey:@"id"];
    }
    [dic setObject:endDataArray[0][0] forKey:@"leadDep"];
    [dic setObject:endDataArray[0][1] forKey:@"timeBatch"];
    [dic setObject:endDataArray[0][2] forKey:@"staffNum"];
    [dic setObject:endDataArray[0][3] forKey:@"staffLiveNum"];
    [dic setObject:endDataArray[0][4] forKey:@"liveFamilyNum"];
    
    [dic setObject:endDataArray[1][0] forKey:@"visitFamilyNum"];
    [dic setObject:endDataArray[1][1] forKey:@"visitNum"];
    
    [dic setObject:endDataArray[2][0] forKey:@"preachNum"];
    [dic setObject:endDataArray[2][1] forKey:@"joinNum"];
    
    [dic setObject:endDataArray[3][0] forKey:@"donation"];
    [dic setObject:endDataArray[3][1] forKey:@"goods"];
    [dic setObject:endDataArray[3][2] forKey:@"doctor"];
    [dic setObject:endDataArray[3][3] forKey:@"goSchool"];
    [dic setObject:endDataArray[3][4] forKey:@"getJob"];
    [dic setObject:endDataArray[3][5] forKey:@"yield"];
    [dic setObject:endDataArray[3][6] forKey:@"other"];
    [dic setObject:endDataArray[3][7] forKey:@"total"];
    [dic setObject:endDataArray[3][8] forKey:@"discountsFamily"];
    
    [dic setObject:endDataArray[4][0] forKey:@"fellowship"];
    [dic setObject:endDataArray[4][1] forKey:@"fellowshipNum"];
    [dic setObject:endDataArray[4][2] forKey:@"actualNum"];
    
    NSLog(@"aaa:%@",dic);
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",RECIPIENTREPORT_ADDORUPDATEREPORT_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:dic success:^(id responseObject) {
        NSLog(@"结亲汇报增加：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([responseObject[@"flag"] integerValue]>0) {
                if (self.editActionBlock) {
                    self.editActionBlock();
                }
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [[ADShowMessage shareShowMessage]showTextOnly:responseObject[@"msg"] showInView:self.view];
            }
        }
    } failure:^(NSError *error) {
        NSLog(@"结亲汇报增加：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = self.dataSource[section];
    return arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOGetMarriedListEditOrCreatViewCell *cell = [MOGetMarriedListEditOrCreatViewCell cellWithTableView:tableView andIndexPath:indexPath];
    MOGetMarriedListEditOrCreatModel *model = self.dataSource[indexPath.section][indexPath.row];
    cell.textFieldEndEditingBlock = ^(NSString * _Nonnull text) {
        model.valueStr = text;
    };
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(54);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==self.dataSource.count-1) {
        return 10;
    }
    return 0.8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 10;
    }
    return kHConversions(56);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return nil;
    }
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHConversions(56))];
    
    NSArray *arr = @[@"走访见面情况",@"宣传政策、法律情况",@"解决困难情况",@"联谊活动开展情况"];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH, backView.height - 10)];
    label.text = arr[section-1];
    label.textColor = KBlackColor_222222;
    label.font = [UIFont boldSystemFontOfSize:15];
    [backView addSubview:label];
    
    return backView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


#pragma mark -懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight - 57 - TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = KGrayColor_f8f8f8;
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}

-(UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin - 57, SCREEN_WIDTH, 57)];
        
        UIButton *submitButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, _bottomView.width, _bottomView.height)];
        [submitButton setTitle:@"提交" forState:UIControlStateNormal];
        [submitButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
        submitButton.titleLabel.font = FONT_SIZE(15);
        [submitButton addTarget:self action:@selector(clickSubmitButton) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:submitButton];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        lineView.backgroundColor = KGrayColor_ededed;
        [_bottomView addSubview:lineView];
    }
    return _bottomView;
}

@end
